package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.ReportForm;

@Component
public class ReportValidator implements Validator{

	@Override
	public boolean supports(Class<?> clazz) {
		return ReportForm.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ReportForm form = (ReportForm) target;	
		if(form.getAfter()==null && !errors.hasFieldErrors("after")){
			errors.rejectValue("after", "empty");
		}
		if(form.getBefore()==null && !errors.hasFieldErrors("before")){
			errors.rejectValue("before", "empty");
		}
		if(!errors.hasFieldErrors())
		{
			if(form.getBefore().after(form.getAfter())){
				errors.rejectValue("after", "switch");
			}
		}
	}

}